package com.example.testapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author
 */
public class home_page extends Fragment {

    private String[] name = new String[]{"热点", "招采", "情报", "答赚", "关注"};
    private ViewPager viewPagerHomePage;
    private TabLayout tabLayoutTitle;
    private TextView tvSearch;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.activity_home_page, container, false);

        tabLayoutTitle = view.findViewById(R.id.titleLine);
        tvSearch = view.findViewById(R.id.tvSearch);
        viewPagerHomePage = view.findViewById(R.id.viewPagerHomePage);

        initPager();
        setTabs(tabLayoutTitle, getLayoutInflater(), name);

        ImageView ivMessage = view.findViewById(R.id.ivMessage);
        ivMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), DownloadPic.class));
            }
        });

        tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SearchHistoryActivity.class));
            }
        });
        return view;
    }

    private void setTabs(TabLayout tabLayout, LayoutInflater inflater, String[] tabTitles) {
        for (int i = 0; i < tabTitles.length; i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            View view = inflater.inflate(R.layout.item_home_page_menu, null);
            // 使用自定义视图，目的是为了便于修改，也可使用自带的视图
            tab.setCustomView(view);

            final TextView tvTitle = view.findViewById(R.id.text_tab);
            tvTitle.setText(tabTitles[i]);
            tabLayout.addTab(tab);
        }
    }

    private void initPager() {
        PagerAdapter adapter = new HomePageAdapter(getChildFragmentManager());
        viewPagerHomePage.setAdapter(adapter);


        viewPagerHomePage.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayoutTitle));
        tabLayoutTitle.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPagerHomePage.setCurrentItem(tab.getPosition(), false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}

